package Work0602;

import java.text.SimpleDateFormat;
import java.util.*;

public class Supermarket {
	private String name;// 超市名
	List<Vip> v = new ArrayList<Vip>();// 创建集合

	public Supermarket(String name) {
		this.name = name;
	}

	public void show() {
		System.out.println("欢迎来到" + name + "超市");
	}

	/**
	 * 开卡
	 * 
	 * @param name
	 * @param password
	 * @return
	 */
	public boolean addcard(String name, String password) {
		if (password.length() >= 6) {// 密码必须不得小于6位
			int cardNumber = (int) (Math.random() * 90000000 + 10000000);// 卡号在10000000~99999999
			Date day = new Date();
			v.add(new Vip(name, cardNumber, password, 100, day));// 增加客户
			System.out.println("开通会员成功,系统赠送您100积分,您的卡号为" + cardNumber);
			return true;
		}
		return false;
	}

	/**
	 * 检查用户名和密码是否存在
	 * 
	 * @param cardNumber
	 * @param password
	 * @return
	 */
	public Vip check(int cardNumber, String password) {
		for (Vip c : v) {
			if (c.getPassword().equals(password) && c.getCardNumber() == cardNumber) {
				return c;
			}
		}
		return null;
	}

	/**
	 * 修改密码
	 * 
	 * @param cardNumber
	 * @param password
	 * @param newpassword
	 * @return
	 */
	public void changePassword(int cardNumber, String password, String newpassword) {
		Vip a = check(cardNumber, password);
		a.setPassword(newpassword);
		System.out.println("修改成功!");
	}

	/**
	 * 累加
	 * 
	 * @param cardNumber
	 * @param password
	 */
	public void accumulation(int cardNumber, String password, int money) {
		Vip a = check(cardNumber, password);
		a.setIntegral(a.getIntegral() + money / 10);
		System.out.println("积分增加成功!");

	}

	/**
	 * 兑换积分
	 * 
	 * @param cardNumber
	 * @param password
	 * @param integral
	 * @return
	 */
	public boolean exchange(int cardNumber, String password, int integral) {
		Vip a = check(cardNumber, password);
		if (integral < 100 || integral % 100 != 0 || integral > a.getIntegral()) {
			return false;
		}
		a.setIntegral(a.getIntegral() - integral);
		return true;
	}

	/**
	 * 查询剩余积分
	 * 
	 * @param cardNumber
	 * @param password
	 * @return
	 */
	public void queryPoints(int cardNumber, String password) {
		Vip a = check(cardNumber, password);
		String str = "yyyy年MM月dd日";
		String str2 = "MM月dd日";
		Date d = new Date();
		System.out.println("姓名    会员卡号    剩余积分    开卡日期");
		if (a.getDay().getYear() == d.getYear()) {
			SimpleDateFormat df = new SimpleDateFormat(str2);
			String a1 = df.format(a.getDay());
			System.out.println(a.getName() + "    " + a.getCardNumber() + "  " + a.getIntegral() + "  " + a1);
		} else {
			SimpleDateFormat df = new SimpleDateFormat(str);
			String a2 = df.format(a.getDay());
			System.out.println(a.getName() + "    " + a.getCardNumber() + "  " + a.getIntegral() + "  " + a2);
		}

	}

}
